import time

from django.test import TestCase

# Create your tests here.


# from gitlabApi import gitlabClient
import gitlabApi


gl = gitlabApi.gitlabClient()


def get_dir_structure(gl, project, path='', tag=None):
    """递归获取项目目录结构"""
    items = []
    try:
        files = project.repository_tree(path=path, recursive=True, all=True, ref=tag)
        for file in files:
            if file['type'] == 'blob':
                items.append({
                    'name': file['name'],
                    'path': file['path'],
                    'type': 'file'
                })
            elif file['type'] == 'tree':
                items.append({
                    'name': file['name'],
                    'path': file['path'],
                    'type': 'directory',
                    'children': get_dir_structure(gl, project, path=file['path'], tag=tag)
                })
    except Exception as e:
        print(f"Error getting directory structure: {e}")
    return items

# project_id = 1  # 替换为实际项目ID
# tag_name = 'v1.0.1'  # 替换为实际Tag名称
#
# # 获取项目对象
# project = gl.projects.get(project_id)
#
# # 获取整个项目在特定Tag下的目录结构
# dir_structure = get_dir_structure(gl, project, tag=tag_name)
# print(dir_structure)


